import * as find from "@geoscene/core/rest/find";
import FindParameters from "@geoscene/core/rest/support/FindParameters";
import * as identify from "@geoscene/core/rest/identify.js";
import IdentifyParameters from "@geoscene/core/rest/support/IdentifyParameters.js";

export const findAttributes = (args: any) => {
  let params = new FindParameters({
    layerIds: args.layerIds,
    searchFields: args.searchFields,
    returnGeometry: args.returnGeometry || false,
    searchText: args.searchText,
  });
  return find.find(args.url, params);
};

export const findByGeometry = (args: any) => {
  let params = new IdentifyParameters({
    layerIds: args.layerIds,
    geometry: args.geometry,
    returnGeometry: args.returnGeometry || false,
  });
  return identify.identify(args.url, params);
};


export function updateSpatialFilter(geometry: any, featureLayer: any) {
  // 使用几何引擎进行空间查询
  featureLayer.queryFeatures({
    geometry: geometry,
    spatialRelationship: "intersects",
    returnGeometry: true,
    outFields: ["*"]
  }).then(function (results: any) {
    // 获取符合空间条件的要素ID
    const objectIds = results.features.map((feature: any) => feature.attributes[featureLayer.objectIdField]);

    // 使用要素ID创建定义表达式
    if (objectIds.length > 0) {
      featureLayer.definitionExpression = `${featureLayer.objectIdField} IN (${objectIds.join(",")})`;
    } else {
      featureLayer.definitionExpression = "1=0"; // 无要素匹配
    }
  });
}
